package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.controllerApi.CategoryControllerApi;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.R;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by ZhaoJun on 2021/11/24 10:55
 */
@Slf4j
@RestController
@RequestMapping("/category")
//@Api(description = "瑞吉外卖分类管理API")
public class CategoryController implements CategoryControllerApi {

    @Autowired
    private CategoryService service;


    @PostMapping
//    @ApiOperation("添加菜品或者套餐的分类")
//    @ApiImplicitParam(name = "category", value = "分类信息", required = true)
    public R addCategory(@RequestBody Category category) {
        log.info("添加分类信息:{}", category);
        service.save(category);

        return R.success("添加成功");
    }

    @GetMapping("/page")
//    @ApiOperation("分页查询分类的信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页码", required = true),
//            @ApiImplicitParam(name = "pageSize", value = "页面条目数", required = true)
//    })
    public R page(Integer page, Integer pageSize) {
        log.info("分页查询:{}", page, pageSize);
        if (page == null || pageSize == null) {

        }

        Page<Category> pageInfo = new Page<>();
        pageInfo.setCurrent(page);
        pageInfo.setSize(pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        service.page(pageInfo, lqw);

        return R.success(pageInfo);
    }

    @DeleteMapping
//    @ApiOperation("根据分类id删除分类信息")
    public R delById(Long id) {
        log.info("删除分类信息:{}", id);
        if (id == null) {
            return R.error("参数有误");
        }

        Category one = service.getById(id);
        if (one == null) {
            return R.error("该数据不存在，删除失败");
        }

        service.delById(id);

        return R.success("删除成功");
    }

    @PutMapping
//    @ApiOperation("根据分类id修改分类的信息")
    public R updateById(@RequestBody Category category) {
        log.info("修改分类信息:{}", category);
        Category one = service.getById(category.getId());
        if (one == null) {
            throw new CustomException("该数据不存在");
        }

        service.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("/list")
//    @ApiOperation("根据分类的类型查询所有对应的分类")
    public R selectByType(Integer type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        if (type != null) {
            lqw.eq(Category::getType, type);
        }
        List<Category> list = service.list(lqw);

        return R.success(list);
    }


}
